﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExpenseSystem.Model;
using ExpenseSystem.Fether;
using ExpenseSystem.DataUpdater;
using ExpenseSystem.Common;
using ExpenseSystem.DataUpdater.Validation;

namespace ExpenseSystem.Core.DeliveryModules
{
    public class DeliveryUpdaterRegister
    {
        int rows;
        public NormalDataUpdater<DeliveryModel> GetDeliveryUpdater()
        {
            NormalDataUpdater<DeliveryModel> systemDataUpdater = new NormalDataUpdater<DeliveryModel>();
            systemDataUpdater.DataUpdating += DataNotSpaceValid;
            systemDataUpdater.DataUpdating += SingletonValid;
            systemDataUpdater.DataUpdating += StateValid;
            systemDataUpdater.DataUpdating += CourierValid;
            systemDataUpdater.DataDeleting += CheckAvaliable;
            return systemDataUpdater;
        }
        private void CheckAvaliable(object sender, DataValidationEventArgs<DeliveryModel> e)
        {
            if (e.Delete.State != Definition.STATE_NORMAL)
            {
                e.Cancle = true;
                e.Message = String.Format("[{0}]正在生效，不能删除", e.Delete.Code);
            }
        }
        private void StateValid(object sender, DataValidationEventArgs<DeliveryModel> e)
        {
            string message;
            if (e.Insert.State != Definition.STATE_NORMAL)
            {
                DeliveryDetailFetcher deliveryDetailFetcher = new DeliveryDetailFetcher();
                if(deliveryDetailFetcher.ListByMainId(e.Insert.Site,e.Insert.AutoId,out message, out rows).Count>0)
                {
                    e.Message = String.Format("物流单{0}必须具备明细信息",e.Insert.Code);
                    e.Cancle = true;
                    return;
                }
                if (e.Insert.PropertyUpdated.Contains("Code") ||
                     e.Insert.PropertyUpdated.Contains("ReceiveDate") ||
                     e.Insert.PropertyUpdated.Contains("CourierId")
                    )
                {
                    e.Message = "已审核的表单不能新增明细行";
                    e.Cancle = true;
                    return;
                }
            }
        }
        private void SingletonValid(object sender, DataValidationEventArgs<DeliveryModel> e)
        {
            DeliveryFetcher DeliveryFetcher = new DeliveryFetcher();
            string message = "";
            DeliveryModel Delivery = DeliveryFetcher.GetByCode( e.Insert.Site, e.Insert.Code, out message);
            if (Delivery != null && Delivery.AutoId != e.Insert.AutoId)
            {
                e.Cancle = true;
                message = String.Format("代号[{0}]已被占用", Delivery.Code);
                return;
            }
        }

        private void DataNotSpaceValid(object sender, DataValidationEventArgs<DeliveryModel> e)
        {
            if (String.IsNullOrWhiteSpace(e.Insert.Code))
            {
                e.Message = "代号不能为空";
                e.Cancle = true;
                return;
            }
            DateTime dt = DateTime.Now;
            if(!DateTime.TryParse(e.Insert.ReceiveDate,out dt))
            {
                e.Message = String.Format("物流单{0}交货日期有误",e.Insert.Code);
                e.Cancle = true;
                return;
            }
        }
        private void CourierValid(object sender, DataValidationEventArgs<DeliveryModel> e)
        {
            string message;
            EmployeeFetcher employeeFetcher = new EmployeeFetcher();
            if(employeeFetcher.Get(e.Insert.Site,e.Insert.CourierId,out message)==null)
            {
                e.Cancle = true;
                e.Message = "物流人员为空";
                return;
            }
        }
    }
}
